<template>
	<view class="container">
		<uni-section title="盘点记录" type="line">
			<view class="example">
	
				<!-- 展示不同的排列方式 -->
				<uni-forms ref="baseForm" :modelValue="baseData" :rules="rules">
				<uni-forms-item label="编码" required name="ProductCode">
					<uni-easyinput v-model="baseData.ProductCode" placeholder="请输入产品编码" @confirm="dosearch" />
				</uni-forms-item>
				<uni-forms-item label="名称" required name="ProductName">
					<uni-easyinput disabled v-model="baseData.ProductName" placeholder="请输入产品名称" />
				</uni-forms-item>
				<uni-forms-item label="规格" required name="Cinvstd">
					<uni-easyinput disabled v-model="baseData.Cinvstd" placeholder="请输入产品规格" />
				</uni-forms-item>
				
				
				<uni-forms-item label="数量" required name="Num">
					<uni-easyinput disabled v-model="baseData.Num" placeholder="请输入数量" />
				</uni-forms-item>
				<uni-forms-item label="实际数量" required name="ActuaNum">
					<uni-easyinput v-model="baseData.ActuaNum" placeholder="请输入数量" />
				</uni-forms-item>
				<uni-forms-item label="单位" required name="CinvmUnit">
					<uni-easyinput disabled v-model="baseData.CinvmUnit" placeholder="请输入单位" />
				</uni-forms-item>
					
				</uni-forms>
				<button type="primary" @click="submit('baseForm')">保存</button>
			</view>
		</uni-section>
	</view>
</template>

<script>
	export default {
		data() {
			return {
			baseData: {
					HeadId: '',
					
					ProductCode: '',
					ProductName: '',
					Cinvstd: '',
					Num: '',
                    ActuaNum:'',
					CinvmUnit: '',
					
				},
				
				focus: false,
				hid: '',
				rules: {
					// 对name字段进行必填验证
					
					ProductCode: {
						// name 字段的校验规则
						rules: [
							// 校验 name 不能为空
							{
								required: true,
								errorMessage: '请填产品编码',
							},
			
						],
			
					},
					ProductName: {
						// name 字段的校验规则
						rules: [
							// 校验 name 不能为空
							{
								required: true,
								errorMessage: '请填写产品名称',
							},
			
						],
			
					},
					Cinvstd: {
						// name 字段的校验规则
						rules: [
							// 校验 name 不能为空
							{
								required: true,
			
								errorMessage: '请填写规格',
							},
			
						],
			
					},
					Num: {
						// name 字段的校验规则
						rules: [
							// 校验 name 不能为空
							{
								required: true,
								errorMessage: '请填写数量',
							},
			
						],
			
					},
					
					ActuaNum: {
						// name 字段的校验规则
						rules: [
							// 校验 name 不能为空
							{
								required: true,
								errorMessage: '请填写实际数量',
							},
								
						],
								
					},
					CinvmUnit: {
						// name 字段的校验规则
						rules: [
							// 校验 name 不能为空
							{
								required: true,
								errorMessage: '请填写单位',
							},
			
						],
			
					},
			
				},
			}
		},
		onLoad(param) {
			this.baseData.HeadId = param.hid
			
		},
		methods: {
			dosearch() {
				
				this.$api.Request({
					url: '/api/TakeStock/Getkc?hid=' + this.baseData.HeadId + '&pcode='+this.baseData.ProductCode,
					method: 'GET',
					data: {},
				}).then((res) => {

					if (res.length == 0) {

						uni.showToast({
							title: `此物料无需盘点，结存为零`,
icon:'error',
						})
						this.focus = true;
					} else {

						//this.baseData.ProductCode = res.ProductCode;
						this.baseData.ProductName = res[0].ProductName;
						this.baseData.Cinvstd = res[0].Cinvstd;
						this.baseData.CinvmUnit = res[0].CinvmUnit;
						this.baseData.Num = res[0].Number;
                        this.baseData.ActuaNum = res[0].Number;
					}

				})
			

			},
			submit(ref) {

				this.$refs[ref].validate().then(res => {
					this.$api.Request({
						url: "/api/TakeStock/SubmitFormbody",
						method: 'POST',
						data: this.baseData,
					}).then((res) => {
						if (res.state == 'success') {
							uni.showToast({
								title: `提交成功`,

							})
							setTimeout(() => {
								uni.navigateTo({
									url: '../takestockbodylist/takestockbodylist?id='+this.baseData.HeadId
								})
							}, 1000)

						} else {

							uni.showToast({
								title: `提交失败，同一个盘点单内，同一个物料只能盘点一次。`,
								icon:'error',
							})
						}
					})

				}).catch(err => {
					console.log('err', err);
				})
			},
			onNavigationBarButtonTap() {
				this.submit('baseForm');
			},

		}
	}
</script>

<style>

</style>
